<template>
  <!-- 分配权限的弹层 -->
  <el-dialog
    title="分配权限(一级为路由页面查看权限-二级为按钮操作权限)"
    :visible="showAssignDialog"
    @close="closeAssignDialog"
  >
    <!-- 权限点数据展示 -->
    <el-tree
      ref="treePointes"
      :data="permissionData"
      :props="{ label: 'name' }"
      :default-expand-all="true"
      :show-checkbox="true"
      :check-strictly="true"
      node-key="id"
    />
    <template #footer>
      <el-button @click="closeAssignDialog">取消</el-button>
      <el-button type="primary" @click="assignPermPoints">确定</el-button>
    </template>
  </el-dialog>
</template>

<script>
//  导入回显分配过的权限点 api + 分配
import { getRoleDetail, assignPerm } from '@/api/setting'
export default {
  props: {
    showAssignDialog: {
      type: Boolean,
      default: false
    },
    permissionData: {
      type: Array,
      default: () => []
    }
  },
  data () {
    return {
      roleId: '' // 点击id
    }
  },
  methods: {
    // 关闭弹窗
    closeAssignDialog () {
      // 清空上次选中
      this.$refs.treePointes.setCheckedKeys([])
      // 子传父
      this.$emit('update:showAssignDialog', false)
    },
    // 获取当前角色之前选中过的权限点
    async showAssign (id) {
      // 存储点击人id
      this.roleId = id
      // 发送请求
      const res = await getRoleDetail(this.roleId)
      // 回填
      this.$refs.treePointes.setCheckedKeys(res.permIds)
    },
    // 点击添加分区配
    async assignPermPoints () {
      // 发送请求
      const data = { id: this.roleId, permIds: this.$refs.treePointes.getCheckedKeys() }
      await assignPerm(data)
      // 提示
      this.$message.success('分配成功宝儿!')
      // 关闭弹窗
      this.closeAssignDialog()
    }
  }

}
</script>

<style lang="scss" scoped>
</style>
